﻿using System;
using System.Web.UI;
using Entities;
using Business;
using System.Linq;
namespace ServicesDirectory.UserControl
{
    /// <summary>
    /// 
    /// </summary>
    public partial class OrganisationDetail1 : System.Web.UI.UserControl
    {
        public string OrgName;
        public string ShortDescription;
        public string ContactId;
        public Guid AddressId;
        public string AddressLine1;
        public string AddressLine2;
        public string AddressLine3;
        public bool OrgPreferred;
        public bool ExpressionOfBusiness;
        public Guid BusinessId;
        public string OrgFullDescription;
        public string PhoneNumber;
        public string Fax;
        public string Email;
        public string WebAddress;
        public string CharityNumber;
        public string CompanyNumber;
        public event EventHandler ExpressionCheckedChange;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            chkExpression.CheckedChanged += ChkExpression_CheckedChanged;
            ListContact1.GetContactId += LookupLeadContact1_GetContactId;
            ListContact1.CloseControl += LookupLeadContact1_CloseControl;
            lbtLookupContact.Click += lbtLookupContact_Click;
            LookupAddress1.GetAddress += LookupAddress1_GetAddress;
            LookupAddress1.CloseControl += LookupAddress1_CloseControl;
            LookupBusinessType1.GetBusiness += LookupBusinessType1_GetBusiness;
            LookupBusinessType1.CloseControl += LookupBusinessType1_CloseControl;
            lbtLookupPostcode.Click += LbtLookupPostcodeClick;
            lbtTypeOfBusiness.Click += lbtTypeOfBusiness_Click;
            txtPostcode.TextChanged += TxtPostcodeTextChanged;
            MessageBox1.Yes += MessageBox1_Yes;
            MessageBox1.No += MessageBox1_No;
        }

        /// <summary>
        /// TXTs the postcode text changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtPostcodeTextChanged(object sender, EventArgs e)
        {
            if (txtPostcode.Text.Length > 0)
            {
                if (OrganizationBusiness.Instance.CheckPostcodeExist(txtPostcode.Text).Count > 0)
                {
                    var address = OrganizationBusiness.Instance.CheckPostcodeExist(txtPostcode.Text).FirstOrDefault();
                    if (address != null)
                    {
                        lbtLookupPostcode.CommandArgument = address.AddressId.ToString();
                        var town = TownBusiness.Instance.GetTown(address.TownId.ToString());
                        if (town != null)
                        {
                            txtCityTown.Text = town.TownName;
                            txtCityTown.Enabled = false;
                        }

                        var county = CountyBusiness.Instance.GetCounty(address.CountyId.ToString());
                        if (county != null)
                        {
                            txtCounty.Text = county.CountyName;
                            txtCounty.Enabled = false;
                        }
                        var country = CountryBusiness.Instance.GetCountry(address.CountryId.ToString());
                        if (country != null)
                        {
                            txtCountry.Text = country.CountryName;
                            txtCountry.Enabled = false;
                        }
                    }
                }
                else
                {
                    MessageBox1.SetTitle("Address Details cannot be confirmed <br/> Do you wish to enter Unconfirmed Address Data (Y/N?)");
                    pnMessage.Visible = true;

                }
            }
        }

        /// <summary>
        /// Handles the No event of the MessageBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void MessageBox1_No(object sender, EventArgs e)
        {
            pnMessage.Visible = false;
            txtPostcode.Focus();
        }

        /// <summary>
        /// Handles the Yes event of the MessageBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void MessageBox1_Yes(object sender, EventArgs e)
        {
            pnMessage.Visible = false;
            //ve nha lam them
            var address = new Address();
            address.AddressId = Guid.NewGuid();
            address.CountryId = Guid.Empty;
            address.CountyId = Guid.Empty;
            address.PostCode = txtPostcode.Text;
            address.Street = string.Empty;
            address.TownId = Guid.Empty;
            var message = AddressBusiness.Instance.Insert(address);
            if (message == "Successfull")
                lbtLookupPostcode.CommandArgument = address.AddressId.ToString();

        }

        /// <summary>
        /// Handles the Click event of the lbtTypeOfBusiness control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtTypeOfBusiness_Click(object sender, EventArgs e)
        {
            pnBusiness.Visible = true;
        }

        /// <summary>
        /// LBTs the lookup postcode click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtLookupPostcodeClick(object sender, EventArgs e)
        {
            pnPostcode.Visible = true;
        }

        /// <summary>
        /// Handles the CloseControl event of the LookupBusinessType1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupBusinessType1_CloseControl(object sender, EventArgs e)
        {
            pnBusiness.Visible = false;
        }

        /// <summary>
        /// Handles the GetBusiness event of the LookupBusinessType1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupBusinessType1_GetBusiness(object sender, EventArgs e)
        {
            var business = (BusinessEntities)sender;
            if (business != null)
            {
                txtBusinessType.Enabled = true;
                lbtTypeOfBusiness.CommandArgument = business.BusinessId.ToString();
                lblSicCode.Text = business.SicCode;
                if (business.BusinessName.Length > 15)
                {
                    txtBusinessType.Text = business.BusinessName.Substring(0, 15) + "...";
                    txtBusinessType.ToolTip = business.BusinessName;
                }
                else
                    txtBusinessType.Text = business.BusinessName;
                txtBusinessType.Enabled = false;
            }
            else
            {
                txtBusinessType.Enabled = true;
                lbtTypeOfBusiness.CommandArgument = Guid.Empty.ToString();
                lblSicCode.Text = string.Empty;

                txtBusinessType.Text = "";


                txtBusinessType.Enabled = false;
            }
        }

        /// <summary>
        /// Handles the CloseControl event of the LookupAddress1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupAddress1_CloseControl(object sender, EventArgs e)
        {
            pnPostcode.Visible = false;
        }

        /// <summary>
        /// Handles the GetAddress event of the LookupAddress1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupAddress1_GetAddress(object sender, EventArgs e)
        {
            var address = (Address)sender;
            if (address != null)
            {
                lbtLookupPostcode.CommandArgument = address.AddressId.ToString();
                txtPostcode.Text = address.PostCode;
                txtCityTown.Text = TownBusiness.Instance.GetTown(address.TownId.ToString()).TownName;
                txtCounty.Text = CountyBusiness.Instance.GetCounty(address.CountyId.ToString()).CountyName;
                txtCountry.Text = CountryBusiness.Instance.GetCountry(address.CountryId.ToString()).CountryName;
            }
            else
            {
                lbtLookupPostcode.CommandArgument = Guid.Empty.ToString();
                txtPostcode.Text = string.Empty;
                txtCityTown.Text = string.Empty;
                txtCounty.Text = string.Empty;
                txtCountry.Text = string.Empty;
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtLookupContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtLookupContact_Click(object sender, EventArgs e)
        {
            
            pnContact.Visible = true;
        }

        /// <summary>
        /// Handles the CloseControl event of the LookupLeadContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupLeadContact1_CloseControl(object sender, EventArgs e)
        {

            pnContact.Visible = false;

        }

        /// <summary>
        /// Handles the GetContactId event of the LookupLeadContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupLeadContact1_GetContactId(object sender, EventArgs e)
        {
            var contact = (Contact)sender;
            if (contact != null)
            {
                lbtLookupContact.CommandArgument = contact.ContactId.ToString();
                lblLeadContact.Text = contact.FirstName + " " + contact.SurName;
            }
            else
            {
                lbtLookupContact.CommandArgument = Guid.Empty.ToString();
                lblLeadContact.Text = "";
            }
        }


        /// <summary>
        /// Handles the CheckedChanged event of the ChkExpression control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ChkExpression_CheckedChanged(object sender, EventArgs e)
        {
            if (ExpressionCheckedChange != null)
                ExpressionCheckedChange(chkExpression.Checked, e);
        }

        /// <summary>
        /// Gets the data.
        /// </summary>
        public void GetData()
        {
            OrgName = txtOrganisationName.Text;
            ShortDescription = txtDescription.Text;
            ContactId = lblLeadContact.Text;
            AddressLine1 = txtAddress1.Text;
            AddressLine2 = txtAddress2.Text;
            AddressLine3 = txtAddress3.Text;
            OrgPreferred = chkPreferred.Checked;
            ExpressionOfBusiness = chkExpression.Checked;
            if (!String.IsNullOrEmpty(lbtTypeOfBusiness.CommandArgument))
                BusinessId = new Guid(lbtTypeOfBusiness.CommandArgument);
            if (!string.IsNullOrEmpty(lbtLookupContact.CommandArgument))
                ContactId = lbtLookupContact.CommandArgument;
            if (!string.IsNullOrEmpty(lbtLookupPostcode.CommandArgument))
                AddressId = new Guid(lbtLookupPostcode.CommandArgument);
            OrgFullDescription = txtFullDescription.Text;
            PhoneNumber = txtPhoneNumber.Text;
            Fax = txtFax.Text;
            Email = txtEmail.Text;
            WebAddress = txtWebAddress.Text;
            CharityNumber = txtCharity.Text;
            CompanyNumber = txtCompany.Text;
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        public void LoadData()
        {
            txtOrganisationName.Text = OrgName;
            txtDescription.Text = ShortDescription;
            string message;
            var contact = ContactBusiness.Instance.GetContact(ContactId, out message);
            if (contact != null)
            {
                lbtLookupContact.CommandArgument = ContactId;
                lblLeadContact.Text = contact.FirstName + " " + contact.SurName;

            }
            var business = BusinessTypeBusiness.Instance.GetBusiness(BusinessId.ToString());
            if (business != null)
            {
                lbtTypeOfBusiness.CommandArgument = BusinessId.ToString();
                if (business.BusinessName.Length > 15)
                {
                    txtBusinessType.Text = business.BusinessName.Substring(0, 15) + "...";
                    txtBusinessType.ToolTip = business.BusinessName;
                }
                else
                    txtBusinessType.Text = business.BusinessName;
                txtBusinessType.Enabled = false;
                lblSicCode.Text = business.SicCode;

            }
            txtAddress1.Text = AddressLine1;
            txtAddress2.Text = AddressLine2;
            txtAddress3.Text = AddressLine3;
            chkPreferred.Checked = OrgPreferred;
            chkExpression.Checked = ExpressionOfBusiness;
            txtFullDescription.Text = OrgFullDescription;
            txtPhoneNumber.Text = PhoneNumber;
            txtFax.Text = Fax;
            txtEmail.Text = Email;
            txtWebAddress.Text = WebAddress;
            txtCharity.Text = CharityNumber;
            txtCompany.Text = CompanyNumber;
            var address = AddressBusiness.Instance.GetAddress(AddressId.ToString());
            if (address != null)
            {
                lbtLookupPostcode.CommandArgument = address.AddressId.ToString();
                txtPostcode.Text = address.PostCode;
                var town = TownBusiness.Instance.GetTown(address.TownId.ToString());
                if (town != null)
                {
                    txtCityTown.Text = town.TownName;

                }
                var county = CountyBusiness.Instance.GetCounty(address.CountyId.ToString());
                if (county != null)
                    txtCounty.Text = county.CountyName;
                var country = CountryBusiness.Instance.GetCountry(address.CountryId.ToString());
                if (country != null)
                    txtCountry.Text = country.CountryName;

            }


        }
    }
}